/*
 * @Descripttion:
 * @Author: Cxy
 * @Date: 2022-12-29 14:18:48
 * @LastEditors: Cxy
 * @LastEditTime: 2024-12-14 21:13:46
 * @FilePath: \xy-admin\play\vite.config.mts
 */
import { URL, fileURLToPath } from 'node:url'
import { resolve } from 'path'
import svgIcon from '@xy-admin/svgicon'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import DefineOptions from 'unplugin-vue-define-options/vite'
import vueJsx from '@vitejs/plugin-vue-jsx'
import svgLoader from 'vite-svg-loader'
import proxy from './vite.proxy'

// https://vitejs.dev/config/
export default defineConfig(config => {
  const env = loadEnv(config.mode, process.cwd(), '')
  return {
    base: env.VITE_BASE,
    mode: config.mode,
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
        imports: ['vue', 'vue-router', 'pinia']
        // dts: 'src/auto-imports.d.ts'
      }),
      Components({
        dirs: ['src/components/*'],
        extensions: ['ts'],
        deep: false,
        resolvers: [ElementPlusResolver()]
      }),
      DefineOptions(),
      svgIcon({
        dir: `${resolve(__dirname, 'src/assets/svg')}`
      }),
      svgLoader()
    ],
    resolve: {
      alias: {
        '@': `${resolve(__dirname, 'src')}`,
        // 此种处理方式不适用 SSR  https://cn.vitejs.dev/guide/assets.html
        assets: fileURLToPath(new URL('./src/assets', import.meta.url)),
        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "@/style/mixin.scss" as *;
            $VITE_MOBILE: ${env.VITE_MOBILE};
          `
        }
      }
    },
    server: {
      host: '0.0.0.0',
      proxy: proxy(config.mode)
    },
    build: {
      sourcemap: false,
      // 消除打包大小超过500kb警告
      chunkSizeWarningLimit: 4000,
      rollupOptions: {
        input: {
          index: `${resolve(__dirname, 'index.html')}`
        },
        // 静态资源分类打包
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
        }
      }
    },
    // 定义常量
    define: {
      'process.env': { ...env, mode: config.mode }
    }
  }
})
